﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;
using Newtonsoft.Json;
using Slja.Model.Common;
using System.Configuration;
namespace Slja.WEB.Web.Project.RunningAccount
{
    public class RunningAccountUtil
    {
        public const string IMPORT_TABLENAME = "f_PrjRunningAccount";
        public RunningAccountUtil(){}
        internal static string DefaultLockColumn = ConfigurationManager.AppSettings["Default_LockColumns"];
        public static string Json4ExtjsFromDatatable(DataTable dt)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{{totalCount:{0},success:true,error:\"\",singleInfo:\"\",data:",dt.Rows.Count);
            //填充数据
            //JsonSerializerSettings setting = new JsonSerializerSettings();
            //setting.
            string strData = JsonConvert.SerializeObject(dt);
            sb.Append(strData);
            sb.Append(",");

            //表格head
            StringBuilder sbhead = new StringBuilder("[");
            //列数组
            StringBuilder sbcolumn = new StringBuilder("[");
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                DataColumn col = dt.Columns[i];
                //sbhead.AppendFormat("{{text:\"{0}\",dataIndex:\"{0}\",sortable:true,editor:}}", col.ColumnName);
                sbhead.Append(GetSerialGridColumn(col.ColumnName, col.ColumnName));
                sbcolumn.AppendFormat("{{name:\"{0}\"}}", col.ColumnName);
                if (i< dt.Columns.Count - 1)
                {
                    sbhead.Append(",");
                    sbcolumn.Append(",");
                }
                else//最后一个
                {
                    sbhead.Append("]");
                    sbcolumn.Append("]");
                }
            }

            sb.Append("columnModel:");
            sb.Append(sbhead.ToString());
            sb.Append(",");
            sb.Append("fieldsNames:");
            sb.Append(sbcolumn.ToString());
            sb.Append("}");
            return sb.ToString();
        }
        public static string GetSerialGridColumn(string text, string dataIndex)
        {
            return GetSerialGridColumn( text, dataIndex,SqlDbType.NVarChar);
        }
        public static string GetSerialGridColumn(string text, string dataIndex, SqlDbType type)
        {
            ExtjsGridColumn gcol = new ExtjsGridColumn();
            gcol.dataIndex = dataIndex;
            gcol.editor = new ExtjsObject("textfield");

            if (DefaultLockColumn.IndexOf("," + text + ",") > -1)
            {
                gcol.locked = true;
            }
            else
            {
                gcol.locked = false;
            }
            //gcol.renderer =
            gcol.sortable = true;
            gcol.text = text;
            gcol.width = 60;
            string str = JsonConvert.SerializeObject(gcol);
            return str;
        }
    }
}